//
//  ViewController.m
//  BluetoothDemo
//
//  Created by Niko on 2025/8/26.
//

#import "ViewController.h"
#import "BluetoothPeripheralManager.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *startButton;
@property (weak, nonatomic) IBOutlet UIButton *stopButton;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@property (weak, nonatomic) IBOutlet UITextField *textField;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self updateUI];
}

- (IBAction)startAdvertisingTapped:(id)sender {
    [[BluetoothPeripheralManager sharedInstance] startAdvertising];
    // UI 更新最好通过 KVO 或通知来监听 isAdvertising 的变化，这里简单处理
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self updateUI];
    });
}

- (IBAction)stopAdvertisingTapped:(id)sender {
    [[BluetoothPeripheralManager sharedInstance] stopAdvertising];
    [self updateUI];
}

- (void)updateUI {
    BOOL isAdvertising = [BluetoothPeripheralManager sharedInstance].isAdvertising;
    self.statusLabel.text = isAdvertising ? @"广播中...." : @"广播已停止";
    self.startButton.enabled = !isAdvertising;
    self.stopButton.enabled = isAdvertising;
}

- (IBAction)textFiledChange:(UITextField *)sender {
    
    
}

- (IBAction)changeBluetoothNameClick:(UIButton *)sender {
    [BluetoothPeripheralManager sharedInstance].bluetoothName = self.textField.text;
    [[BluetoothPeripheralManager sharedInstance] stopAdvertising];
    [self updateUI];
}


@end
